package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.adapter.CareOrderAdapter;
import com.mengchengnet.chaperonage.domain.base.Order;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;
import com.mengchengnet.chaperonage.view.refreshableListView.RefreshableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/29.
 */

public class CareOrderActivity extends BaseActivity implements View.OnClickListener{
    private static final String TAG = "CareOrderActivity";
    private RequestQueue requestQueue;
    private Button btn1,btn2,btn3,btn4;
    private View view1,view2,view3,view4;
    private RefreshableListView lv1,lv2,lv3,lv4;
    private String userId;//陪护师的id
    private CareOrderAdapter orderAdapter;
    private ArrayList<Order> orderList;
    private String mark;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    orderAdapter = new CareOrderAdapter(getApplicationContext(), R.layout.item_order_listview, orderList);
                    lv1.setPullLoadEnable(false);
                    lv1.setPullRefreshEnable(false);
                    lv1.setAdapter(orderAdapter);
                    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                        }
                    });

                    break;
                case 1:
                    orderAdapter = new CareOrderAdapter(getApplicationContext(), R.layout.item_order_listview, orderList);
                    lv2.setPullLoadEnable(false);
                    lv2.setPullRefreshEnable(false);
                    lv2.setAdapter(orderAdapter);
                    break;
                case 2:
                    orderAdapter = new CareOrderAdapter(getApplicationContext(), R.layout.item_order_listview, orderList);
                    lv3.setPullLoadEnable(false);
                    lv3.setPullRefreshEnable(false);
                    lv3.setAdapter(orderAdapter);

                    break;
                case 3:
                    orderAdapter = new CareOrderAdapter(getApplicationContext(), R.layout.item_order_listview, orderList);
                    lv4.setPullLoadEnable(false);
                    lv4.setPullRefreshEnable(false);
                    lv4.setAdapter(orderAdapter);

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_careorder);
        toolbarState = TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("我的订单");
        requestQueue= Volley.newRequestQueue(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mark=getIntent().getStringExtra("key");
        if (mark.equals("0")){
            btn1.setTextColor(getResources().getColor(R.color.blue_baby));
            btn2.setTextColor(getResources().getColor(R.color.black));
            btn3.setTextColor(getResources().getColor(R.color.black));
            btn4.setTextColor(getResources().getColor(R.color.black));
            view1.setBackgroundResource(R.color.blue_baby);
            view2.setBackgroundResource(R.color.colorGray);
            view3.setBackgroundResource(R.color.colorGray);
            view4.setBackgroundResource(R.color.colorGray);
            lv1.setVisibility(View.VISIBLE);
            lv2.setVisibility(View.GONE);
            lv3.setVisibility(View.GONE);
            lv4.setVisibility(View.GONE);
            getOrder("0",0);
        }else if(mark.equals("1")){
            btn1.setTextColor(getResources().getColor(R.color.black));
            btn2.setTextColor(getResources().getColor(R.color.blue_baby));
            btn3.setTextColor(getResources().getColor(R.color.black));
            btn4.setTextColor(getResources().getColor(R.color.black));
            view1.setBackgroundResource(R.color.colorGray);
            view2.setBackgroundResource(R.color.blue_baby);
            view3.setBackgroundResource(R.color.colorGray);
            view4.setBackgroundResource(R.color.colorGray);
            lv1.setVisibility(View.GONE);
            lv2.setVisibility(View.VISIBLE);
            lv3.setVisibility(View.GONE);
            lv4.setVisibility(View.GONE);
            getOrder("1",1);
        }else if(mark.equals("2")){
            btn1.setTextColor(getResources().getColor(R.color.black));
            btn2.setTextColor(getResources().getColor(R.color.black));
            btn4.setTextColor(getResources().getColor(R.color.black));
            btn3.setTextColor(getResources().getColor(R.color.blue_baby));
            view1.setBackgroundResource(R.color.colorGray);
            view2.setBackgroundResource(R.color.colorGray);
            view4.setBackgroundResource(R.color.colorGray);
            view3.setBackgroundResource(R.color.blue_baby);
            lv1.setVisibility(View.GONE);
            lv2.setVisibility(View.GONE);
            lv4.setVisibility(View.GONE);
            lv3.setVisibility(View.VISIBLE);
            getOrder("2",2);
        }
        else if(mark.equals("3")){
            btn1.setTextColor(getResources().getColor(R.color.black));
            btn2.setTextColor(getResources().getColor(R.color.black));
            btn3.setTextColor(getResources().getColor(R.color.black));
            btn4.setTextColor(getResources().getColor(R.color.blue_baby));
            view1.setBackgroundResource(R.color.colorGray);
            view2.setBackgroundResource(R.color.colorGray);
            view3.setBackgroundResource(R.color.colorGray);
            view4.setBackgroundResource(R.color.blue_baby);
            lv1.setVisibility(View.GONE);
            lv2.setVisibility(View.GONE);
            lv3.setVisibility(View.GONE);
            lv4.setVisibility(View.VISIBLE);
            getOrder("3",3);
        }
    }

    private void initView() {
        userId= PreferenceUtil.getString(this,"userId");
        orderList = new ArrayList<>();
        Log.e(TAG, "userId: "+userId);
        btn1= (Button) findViewById(R.id.careOrder_btn1);
        btn2= (Button) findViewById(R.id.careOrder_btn2);
        btn3= (Button) findViewById(R.id.careOrder_btn3);
        btn4= (Button) findViewById(R.id.careOrder_btn4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        view1=findViewById(R.id.careOrder_view1);
        view2=findViewById(R.id.careOrder_view2);
        view3=findViewById(R.id.careOrder_view3);
        view4=findViewById(R.id.careOrder_view4);

        lv1= (RefreshableListView) findViewById(R.id.careOrder_lv1);
        lv2= (RefreshableListView) findViewById(R.id.careOrder_lv2);
        lv3= (RefreshableListView) findViewById(R.id.careOrder_lv3);
        lv4= (RefreshableListView) findViewById(R.id.careOrder_lv4);

        lv1.setVisibility(View.VISIBLE);
        lv2.setVisibility(View.GONE);
        lv3.setVisibility(View.GONE);
        lv4.setVisibility(View.GONE);
        getOrder("0",0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.careOrder_btn1:
                btn1.setTextColor(getResources().getColor(R.color.blue_baby));
                btn2.setTextColor(getResources().getColor(R.color.black));
                btn3.setTextColor(getResources().getColor(R.color.black));
                btn4.setTextColor(getResources().getColor(R.color.black));
                view1.setBackgroundResource(R.color.blue_baby);
                view2.setBackgroundResource(R.color.colorGray);
                view3.setBackgroundResource(R.color.colorGray);
                view4.setBackgroundResource(R.color.colorGray);
                lv1.setVisibility(View.VISIBLE);
                lv2.setVisibility(View.GONE);
                lv3.setVisibility(View.GONE);
                lv4.setVisibility(View.GONE);
                getOrder("0",0);
                break;
            case R.id.careOrder_btn2:
                btn1.setTextColor(getResources().getColor(R.color.black));
                btn2.setTextColor(getResources().getColor(R.color.blue_baby));
                btn3.setTextColor(getResources().getColor(R.color.black));
                btn4.setTextColor(getResources().getColor(R.color.black));
                view1.setBackgroundResource(R.color.colorGray);
                view2.setBackgroundResource(R.color.blue_baby);
                view3.setBackgroundResource(R.color.colorGray);
                view4.setBackgroundResource(R.color.colorGray);
                lv1.setVisibility(View.GONE);
                lv2.setVisibility(View.VISIBLE);
                lv3.setVisibility(View.GONE);
                lv4.setVisibility(View.GONE);
                getOrder("1",1);
                break;
            case R.id.careOrder_btn3:
                btn1.setTextColor(getResources().getColor(R.color.black));
                btn2.setTextColor(getResources().getColor(R.color.black));
                btn4.setTextColor(getResources().getColor(R.color.black));
                btn3.setTextColor(getResources().getColor(R.color.blue_baby));
                view1.setBackgroundResource(R.color.colorGray);
                view2.setBackgroundResource(R.color.colorGray);
                view4.setBackgroundResource(R.color.colorGray);
                view3.setBackgroundResource(R.color.blue_baby);
                lv1.setVisibility(View.GONE);
                lv2.setVisibility(View.GONE);
                lv4.setVisibility(View.GONE);
                lv3.setVisibility(View.VISIBLE);
                getOrder("2",2);
                break;
            case R.id.careOrder_btn4:
                btn1.setTextColor(getResources().getColor(R.color.black));
                btn2.setTextColor(getResources().getColor(R.color.black));
                btn3.setTextColor(getResources().getColor(R.color.black));
                btn4.setTextColor(getResources().getColor(R.color.blue_baby));
                view1.setBackgroundResource(R.color.colorGray);
                view2.setBackgroundResource(R.color.colorGray);
                view3.setBackgroundResource(R.color.colorGray);
                view4.setBackgroundResource(R.color.blue_baby);
                lv1.setVisibility(View.GONE);
                lv2.setVisibility(View.GONE);
                lv3.setVisibility(View.GONE);
                lv4.setVisibility(View.VISIBLE);
                getOrder("3",3);
                break;
        }
    }

    private void getOrder(final String status, final int code) {
        StringRequest request = new StringRequest(Request.Method.POST, Url.GetOrderList,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e(TAG, "陪护订单: "+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }

                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                JSONArray array=jsonObject.getJSONArray("orderList");
                                orderList.clear();
                                for (int i = 0; i <array.length() ; i++) {
                                    JSONObject object=array.getJSONObject(i);
                                    String progress=object.getString("progress");
                                    String totalMoney=object.getString("totalMoney");
                                    String startTime=object.getString("startTime");
                                    String unit_price=object.getString("unit_price");
                                    String ServiceName=object.getString("ServiceName");
                                    String icon=object.getString("icon");
                                    String orderId=object.getString("orderId");
                                    String personId=object.getString("personId");


                                    Order order = new Order();
                                    order.setUserId(personId);
                                    order.setOrderId(orderId);
                                    order.setChaperonagePhoto(icon);
                                    order.setBusinessType("陪护");
                                    order.setChaperonageName(ServiceName);
                                    order.setOrderState(progress);
                                    order.setPrice(unit_price+"/天");
                                    order.setTotalPrice(totalMoney);
                                    order.setServeTime(startTime);
                                    orderList.add(order);

                                }
                                handler.sendEmptyMessage(code);
                            }
                            else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("loginId",userId);
                map.put("status",status);

                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
